package org.example.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.example.common.R;
import org.example.entity.Cafeteria;
import org.example.service.CafeteriaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * TODO 类描述
 *
 * @author shaoxuan.
 * @date 2024/1/26 18:29
 */
@RestController
@RequestMapping("/cafeteria")
@Slf4j
@Api(tags = "菜品管理")
public class CafeteriaController {

    @Autowired
    private CafeteriaService service;

    @ApiOperation("获取所有菜品")
    @GetMapping("/getAll")
    public R<List<Cafeteria>> getAll() {
        return R.success(service.list());
    }

    @ApiOperation("根据ID获取菜品")
    @GetMapping("/getById/{id}")
    public R<Cafeteria> getById(@PathVariable("id") String id) {
        return R.success(service.getBaseMapper().selectById(id));
    }


    @ApiOperation("添加菜品")
    @PostMapping("/add")
    public R<Cafeteria> add(@RequestBody Cafeteria cafeteria) {
        boolean save = service.save(cafeteria);
        if (save) {
            return R.success(cafeteria).setMsg("添加成功");
        } else {
            return R.error("添加失败");
        }
    }

    @ApiOperation("修改菜品")
    @PutMapping("/update")
    public R<Cafeteria> update(@RequestBody Cafeteria cafeteria){
        if(service.updateById(cafeteria)){
            return R.success(cafeteria).setMsg("修改成功");
        }else {
            return R.error("修改失败");
        }
    }

    @ApiOperation("删除菜品")
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") String id){
        if(service.removeById(id)){
            return R.success(null).setMsg("删除成功");
        }else {
            return R.error("删除失败");
        }
    }
}
